﻿using UnityEngine;
using System.Collections;

public class PlayerBlockScript : MonoBehaviour {

	public bool StartScreen = false;
	int nrOfSegments = 0;


	Vector3 prevTrailPosition;
	public float DistanceBetweenTrailSegments = 1;
	public GameObject trailObject;
	// Use this for initialization
	void Start () {

		prevTrailPosition = transform.position;

		if(StartScreen)
		{
			rigidbody.AddForce( new Vector3(400,400,0));
			rigidbody.AddTorque(new Vector3(0,0,-48.819f));
		}
	}
	
	// Update is called once per frame
	void Update () 	
	{
		UpdateTrail();


	}


	void UpdateTrail()
	{
		if((prevTrailPosition - transform.position).magnitude > 1)
		{
			SpawnTrail();
			nrOfSegments++;

			if(StartScreen && nrOfSegments > 12)
			{
				//rigidbody.isKinematic = true;
			}
		}



	}


	void SpawnTrail()
	{
		Quaternion q = new Quaternion();
		//q.SetLookRotation((prevTrailPosition - transform.position).normalized);

		q.SetLookRotation(rigidbody.velocity);
		GameObject newObj = (GameObject)Instantiate(trailObject, transform.position, q);
		newObj.SetActive(true);
		prevTrailPosition = transform.position;
	
	}

}
